package com.copote.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: CommFunc
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: 湖南湘邮科技股份有限公司/RedStar
 * @date: 2023/9/18 11:14
 * @Copyright: 2019 2000 - 2022 HuNan Copote Science & Technology CO., LTD. All Rights Reserved.<br/>
 * 注意：本内容仅限于湖南湘邮科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
@Slf4j
public class CommFunc {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat y_m_d = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat ymd = new SimpleDateFormat("yyyy/MM/dd");
    private static SimpleDateFormat HMS = new SimpleDateFormat("HH:mm:ss");
    private static SimpleDateFormat Hms = new SimpleDateFormat("HHmmss");
    public static String getNowDayTime() {
        return "收货时间：" + getNowDate();
    }

    public static String getNowYMD() {
        return ymd.format(new Date());
    }

    public static Date getNowDay(String strDate) {
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getNowDate() {
        return sdf.format(new Date());
    }
    public static String getNowHMS() {
        return HMS.format(new Date());
    }
    public static String getNowHms() {
        return Hms.format(new Date());
    }

    public static String getStrDate(Date date) {
        return sdf.format(date);
    }

    /**
     * 计算字符串所占字符位，不是字节数
     *
     * @param str
     * @return
     */
    public static int getStrLength(String str) {
        if (StringUtils.isEmpty(str)) {
            return 0;
        }
        int valueLength = 0;
        for (int i = 0; i < str.length(); i++) {
            char temp = str.charAt(i);
            if ((temp + "").getBytes().length == 1 && ' ' != temp) {
                valueLength += 1;
            } else {
                valueLength += 2;
            }
        }
        return valueLength;
    }

    public static void mkDir(String path) {
        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
                file.mkdirs();
            }
        } else {
            file.mkdirs();
        }
    }

    public static void main(String[] args) throws IOException {
        String str = "1234陕西";
        String str1 = "1234 陕西";
        String str2 = "87\t-\t10kg宁乡黑土猪板油";
        log.info("{}--{}", str.getBytes().length, getStrLength(str));
        log.info("{}--{}", str1.getBytes().length, getStrLength(str1));
        log.info("{}--{}", str2.getBytes().length, getStrLength(str2));

    }
}
